package testcase.app;

import static org.testng.Assert.assertEquals;
import java.io.File;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import api.app.ApiUploadPicforOnlineConsultation;
import common.Host;
import common.ImageOperation;
import common.TestData;

public class TestApiUploadPicforOnlineConsultation{
	/**
	 * 帮忙医APP上传咨询图片ApiUploadPicforOnlineConsultation接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */	 
	@DataProvider(name="Test")
	public static String[][] testApiUploadPicforOnlineConsultationDataProvider()throws Exception
	{
		Host.setup("Test");
		TestData td=new TestData("app/TestApiUploadPicforOnlineConsultation.xml");
		return td.getParam();
	}
	
	/**
	 * 帮忙医APP上传咨询图片ApiUploadPicforOnlineConsultation接口测试用例
	 * @param caseName-用例名称
	 * @param image-图片
	 * @throws Exception
	 */	 
	@Test(dataProvider="Test",groups={"Test"})
    public void testApiUploadPicforOnlineConsultationTestCase(String caseName,String image)throws Exception
	{
		String result=null;	
		if (!image.isEmpty())
		{
		String[] imageName = image.split(",");//存储图片名称
		String[] base64Image = new String[imageName.length];//存储图片编码
		for(int i=0; i<base64Image.length; i++)
		{
			//获取图片Base64编码字符串
			String imagePath = System.getProperty("user.dir")
			+ File.separator + "resources"
			+ File.separator + "image"
			+ File.separator + imageName[i];
			base64Image[i] = ImageOperation.base64Encode(imagePath);
		}			
		//调用接口并将接口返回数据转换成json格式
		result = ApiUploadPicforOnlineConsultation.post(base64Image);
		}
		else
		{
		result=ApiUploadPicforOnlineConsultation.post(null);
		}
		JSONObject jsonResult;
		if (result==null || result.isEmpty())
		{
		jsonResult = null;
		}
		else
		{
		jsonResult = new JSONObject(result);
		}	
		
		//设置检查点
		String checkpoint;
		switch (caseName) 
		{		
		case "UpLoad1Pic":
		checkpoint = jsonResult.getJSONArray("path").getString(0);		
		    Assert.assertFalse(checkpoint.isEmpty());		
		    assertEquals(jsonResult.getString("Message"),"上传成功");
		    break;
		case "UpLoad5Pic":			
		for (int i=0;i<jsonResult.getJSONArray("path").length();i++)
			{
			    checkpoint = jsonResult.getJSONArray("path").getString(i);
				Assert.assertFalse(checkpoint.isEmpty());
			}
			assertEquals(jsonResult.getString("Message"), "上传成功");
			break;
		case "UpLoadNoPic":
			checkpoint = jsonResult.getJSONArray("path").getString(0);	
			Assert.assertTrue(checkpoint.isEmpty());
			assertEquals(jsonResult.getString("Message"), "上传成功");
			break;
		default:
			break;
		}
	}
	
}


	




